import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import RolesList from '../components/right/RolesList.vue'
import RightsList from '../components/right/RightsList.vue'
import GoodsCate from '../components/good/GoodsCate.vue'
import CateParams from '../components/good/CateParams.vue'
import GoodsList from '../components/good/GoodsList.vue'
import AddGood from '../components/good/AddGood.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },

  {
    path: '/login',
    component: Login,
  },

  {
    path: '/home',
    component: Home,
    redirect:'/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome,
      },
      {
        path:'/users',
        component: Users,
      },
      {
        path:'/roles',
        component:RolesList,
      },
      {
        path:'/rights',
        component:RightsList,
      },
      {
        path:'/categories',
        component:GoodsCate,
      },
      {
        path:'/params',
        component:CateParams,
      },      
      {
        path:'/goods',
        component:GoodsList,
      }, 
      {
        path:'/goods/add',
        component: AddGood
      },
      {
        path:'/orders',
        component: Order
      },
      {
        path:'/reports',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to :将要访问的路径
  // from：代表从哪个页面跳转过来
  // next是一个函数，表示放行
  // 如果用户要访问login页面，直接放行,不传参代表该去哪就去哪，传参表示去参数路径
  if (to.path === '/login') return next();
  const token = window.sessionStorage.getItem('token');
  if (!token) {
    return next('/login');
  }
  next();
})

export default router
